#include "StdAfx.h"
#include ".\favi.h"

fAvi::fAvi(void)
{
	m_posAvi = 0;
	m_delay = 0;
}


fAvi::~fAvi(void)
{
	cvDestroyWindow(m_strOriginWindow);
}

int fAvi::closeAvi(void)
{
	cvReleaseCapture( &m_pCapture );
	delete m_pFD;
	return 1;
}


void fAvi::TrackbarCallback(int pos)
{
	m_posAvi = pos;
}

int fAvi::openAvi(void)
{
	if (m_pFD->DoModal() == IDOK)
	{
		int value = 0;
		m_pCapture = cvCaptureFromFile(m_pFD->GetFileName());
		m_strOriginWindow = m_pFD->GetFileName();
		cvNamedWindow(m_strOriginWindow, CV_WINDOW_AUTOSIZE);

		//cvCreateTrackbar(m_strOriginWindow + "FRAME", m_strOriginWindow, &value, 10000, TrackbarCallback);
		//cvGrabFrame(m_pCapture);
		m_posAvi = 0;
	}

	return 1;
}


int fAvi::loadAviImage(void)
{
	double pos;
	
	cvGrabFrame(m_pCapture);
	if (!m_pCapture)
		return -1;
	//cvSetCaptureProperty( m_pCapture, CV_CAP_PROP_POS_FRAMES, m_posAvi);
	m_pSrcImage = cvRetrieveFrame(m_pCapture);
	
	if (!m_pSrcImage)
		return -1;
	pos = cvGetCaptureProperty( m_pCapture, CV_CAP_PROP_POS_FRAMES);
	
	m_posAvi = pos;	
	return 1;
}

int fAvi::ShowOrginImage(void)
{
	cvShowImage( m_strOriginWindow, m_pSrcImage);
	return 1;
}

double fAvi::calDelay(void)
{
	m_delay = cvGetCaptureProperty(m_pCapture, CV_CAP_PROP_FPS);
	/*CString debugMsg;
	debugMsg.Format("m_delay %f\n", m_delay);
	OutputDebugString(debugMsg);
*/
	m_delay = 1 / m_delay * 20;
	if (m_delay < 1.0)
		m_delay = 1.0;
	
	return m_delay;
}

double fAvi::getFps(void)
{
	return (cvGetCaptureProperty(m_pCapture, CV_CAP_PROP_FPS));
}

